package com.megvii.corelib.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by linyue on 16/1/5.
 */
public class SharedPreferenceTool
{
    private static final String FILE_NAME = "megvii";

    public static String getStringValueByKey(Context context, String key, String defaultValue)
    {
        SharedPreferences sharePre = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sharePre.getString(key, defaultValue);
    }

    public static void saveStringValue(Context context, String key, String value)
    {
        SharedPreferences sharePre = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharePre.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static void removeStringValue(Context context, String key)
    {
        SharedPreferences sharePre = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharePre.edit();
        editor.remove(key);
        editor.commit();
    }

    public static Boolean getBooleanValueByKey(Context context, String key, boolean defaultValue)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    public static void saveBooleanValue(Context context, String key, boolean value)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static int getIntValueByKey(Context context, String key, int defaultValue)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key, defaultValue);
    }

    public static float getFloatValueByKey(Context context, String key, float defaultValue)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        return sharedPreferences.getFloat(key, defaultValue);
    }

    public static void saveFloatValue(Context context, String key, float value)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public static void saveIntValue(Context context, String key, int value)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static Collection<Integer> getIntegerCollection(Context context, String key)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        Set<String> stringSet = sharedPreferences.getStringSet(key, null);
        if (stringSet == null)
        {
            return null;
        }
        else
        {
            Collection<Integer> result = new ArrayList<>();
            for (String value : stringSet)
            {
                result.add(Integer.valueOf(value));
            }
            return result;
        }
    }

    public static void saveIntegerCollection(Context context, String key,
            Collection<Integer> collection)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (collection != null)
        {
            Set<String> set = new HashSet<>();
            for (Integer value : collection)
            {
                set.add(String.valueOf(value));
            }
            editor.putStringSet(key, set);
            editor.commit();
        }
    }

    public static Collection<String> getStringCollection(Context context, String key)
    {
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        Set<String> stringSet = sharedPreferences.getStringSet(key, null);
        return stringSet;
    }

    public static void saveStringCollection(Context context, String key,
            Collection<String> collection)
    {
        if (collection != null || collection.isEmpty())
        {
            SharedPreferences sharedPreferences = context
                    .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            Set<String> set = new HashSet<>();
            set.addAll(collection);
            editor.putStringSet(key, set);
            editor.commit();
        }
        else
        {
            removeStringCollection(context, key);
        }
    }

    public static void removeStringCollection(Context context, String key)
    {
        SharedPreferences sharePre = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharePre.edit();
        editor.remove(key);
        editor.commit();
    }
}
